﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using OpenExpressApp.Module.WPF;
using Csla.Core;
using OpenExpressApp.Module.WPF.Converter;
using OpenExpressApp.Editors;
using OpenExpressApp.MetaModel;

namespace OpenExpressApp.Module.WPF.Editors
{
    class GDataGridEnumColumn : OpenDataGridColumn
    {
        private EnumPropertyEditor _editor;

        public GDataGridEnumColumn(BusinessObjectPropertyInfo info, IListObjectView view)
            : base(info, view)
        {
            this._editor = new EnumPropertyEditor(this.PropertyInfo, this.View);
        }

        public override IWPFPropertyEditor Editor
        {
            get
            {
                return this._editor;
            }
        }

        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            TextBlock tb = new TextBlock();
            Binding textBinding = new Binding(PropertyInfo.Name);
            textBinding.Converter = new EnumConverter();
            tb.SetBinding(TextBlock.TextProperty, textBinding);
            return tb;
        }
        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            FrameworkElement result = base.GenerateEditingElement(cell, dataItem);

            ComboBox comboBox = result as ComboBox;
            if (comboBox != null)
            {
                comboBox.SelectedIndex = comboBox.Items.IndexOf(new EnumViewModel(this._editor.PropertyValue as Enum));
            }

            return result;
        }
    }
}
